package com.ease.web.xwork2;

import java.lang.reflect.Method;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 
 * @author nathanleewei
 * 
 */
public class ParametersPrepareInterceptor extends MethodFilterInterceptor {

	/**
	 *
	 */
	private static final long serialVersionUID = 2663116944723250867L;

	public String doIntercept(ActionInvocation invocation) throws Exception {

		Object action = invocation.getAction();
		String methodName = invocation.getProxy().getMethod();

		Method method = null;
		// TODO handle exception
		// try {
		method = action.getClass().getMethod(methodName, new Class[0]);

		if (method.isAnnotationPresent(ParametersPreparable.class)) {

			ParametersPreparable parametersPreparable = method.getAnnotation(ParametersPreparable.class);
			String parametersPrepareMethodName = parametersPreparable.method();

			Method parametersPrepareMethod = action.getClass().getMethod(parametersPrepareMethodName, new Class[0]);
			parametersPrepareMethod.invoke(action, new Object[0]);

		}
		// } catch (NoSuchMethodException e) {
		//
		// }

		return invocation.invoke();
	}
}
